package cn.tedu.boot6.controller;

import cn.tedu.boot6.entity.Image;
import cn.tedu.boot6.mapper.ImageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
public class ImageController {
    @Autowired
    ImageMapper mapper;
    @RequestMapping("/insert")
    public void insert(@RequestBody Image image){
        System.out.println("image = " + image);
        mapper.insert(image);
    }
    @RequestMapping("/select")
    public List<Image> select(){
        return mapper.select();
    }

    @RequestMapping("/delete")
    public void delete(int id){
        //通过id查询到url
        String url = mapper.selectUrlById(id);
        //得到图片的完整路径
        String filePath = "D:/files"+url;
        new File(filePath).delete();
        //删除数据库里面的数据
        mapper.deleteById(id);
    }

}
/*
create table image(id int primary key auto_increment,intro varchar(100),
url varchar(200));
* */